import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

// 引入路由所映射的组件
import Login from '@/views/Login.vue'
import Personal from '@/views/personal/index.vue'
import EditPersonal from '@/views/personal/editPersonal.vue'

// 创建路由对象
let router = new VueRouter({
    // 配置路由
    routes: [{
            name: 'Login',
            path: '/login',
            component: Login
        },
        {
            name: 'Personal',
            path: '/personal/:id',
            component: Personal
        },
        {
            name: 'editPersonal',
            path: '/editPersonal/:id',
            component: EditPersonal
        }
    ]
})

// 添加导航守卫
router.beforeEach((to, from, next) => {
    if (to.name == 'personal') {
        let token = localStorage.getItem('hm_toutiao_52')
        if (token) {
            next()
        } else {
            next({
                path: '/login'
            })
        }
    } else {
        next()
    }
})

// 暴露
export default router